#region Using Statements
using System;
using Microsoft.Xna.Framework;
using System.Threading;
using System.Globalization;
#endregion

namespace SpaceGame
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    /// <remarks>
    /// This class is similar to one of the same name in the GameStateManagement sample.
    /// </remarks>
    class OptionsMenuScreen : PhoneMenuScreen
    {
        #region Fields


        static string[] asteroidDensity = { "None", "Low", "Medium", "High" };
        static int currentAsteroidDensity = 2;

        static string[] wallStyle = { "None", "One", "Two", "Three" };
        static int currentWallStyle = 0;

        static int scoreLimit = 10;
        static bool motionBlur = true;
        static bool neonEffect = true;

		Button scoreLimitButton = new Button("score limit");
		Button asteroidDensityButton = new Button("asteroid density");
		Button wallStyleButton = new Button("Walls style");
		BooleanButton motionBlurButton = new BooleanButton("Blur", false);
		BooleanButton neonEffectButton = new BooleanButton("Neon Effect", false);
		Button localeButton = new Button(String.Empty);

		Button backButton = new Button("Back");

		#region menu entries
		//MenuEntry scoreLimitMenuEntry = new MenuEntry(String.Empty);
		//MenuEntry asteroidDensityMenuEntry = new MenuEntry(String.Empty);
		//MenuEntry wallStyleMenuEntry = new MenuEntry(String.Empty);
		//MenuEntry motionBlurMenuEntry = new MenuEntry(String.Empty);
		//MenuEntry neonEffectMenuEntry = new MenuEntry(String.Empty);
		#endregion

		//For Debugging
        static bool showMeshes = true;
        MenuEntry showMeshesMenuEntry = new MenuEntry(String.Empty);


        #endregion

        #region Initialization


        /// <summary>
        /// Constructor populates the menu with empty strings: the real values
        /// are filled in by the Update method to reflect the changing settings.
        /// </summary>
        public OptionsMenuScreen() : base(StringData.options_text)
        {
            currentAsteroidDensity = (int)WorldRules.AsteroidDensity;
            currentWallStyle = (int)WorldRules.WallStyle;
            scoreLimit = WorldRules.ScoreLimit;
            motionBlur = WorldRules.MotionBlur;
            neonEffect = WorldRules.NeonEffect;

			#region Old
			//scoreLimitMenuEntry.Selected += ScoreLimitMenuEntrySelected;
			//asteroidDensityMenuEntry.Selected += AsteroidDensityMenuEntrySelected;
			//wallStyleMenuEntry.Selected += WallStyleMenuEntrySelected;
			//motionBlurMenuEntry.Selected += MotionBlurMenuEntrySelected;
			//neonEffectMenuEntry.Selected += NeonEffectMenuEntrySelected;

			//MenuEntries.Add(scoreLimitMenuEntry);
			//MenuEntries.Add(asteroidDensityMenuEntry);
			//MenuEntries.Add(wallStyleMenuEntry);
			//MenuEntries.Add(motionBlurMenuEntry);
			//MenuEntries.Add(neonEffectMenuEntry);
			#endregion

			scoreLimitButton.Tapped += new EventHandler<EventArgs>(scoreLimitButton_Tapped);
			localeButton.Tapped += new EventHandler<EventArgs>(localeButton_Tapped);
			backButton.Tapped += new EventHandler<EventArgs>(backButton_Tapped);

			MenuButtons.Add(scoreLimitButton);
			MenuButtons.Add(localeButton);
			MenuButtons.Add(backButton);

            showMeshes = WorldRules.ShowMeshes;
            showMeshesMenuEntry.Selected += delegate{ showMeshes = !showMeshes; };
			//MenuEntries.Add(showMeshesMenuEntry);
        }

		void backButton_Tapped(object sender, EventArgs e)
		{
			WorldRules.ScoreLimit = scoreLimit;
			ExitScreen();
		}
        #endregion

        #region Handle Input

		void localeButton_Tapped(object sender, EventArgs e)
		{
			// Tell the resource manager what language to use when loading strings.
			if (Thread.CurrentThread.CurrentUICulture.Name == "en-US")
			{
				// Changes the CurrentCulture of the current thread to de-DE.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());
				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
			else if (Thread.CurrentThread.CurrentUICulture.Name == "de-DE")
			{
				// Changes the CurrentCulture of the current thread to fr-FR.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());
				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
			else if (Thread.CurrentThread.CurrentUICulture.Name == "fr-FR")
			{
				// Changes the CurrentCulture of the current thread to ru-RU.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());
				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
			else if (Thread.CurrentThread.CurrentUICulture.Name == "it-IT")
			{
				// Changes the CurrentCulture of the current thread to uk-UA.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());

				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
			else if (Thread.CurrentThread.CurrentUICulture.Name == "ru-RU")
			{
				// Changes the CurrentCulture of the current thread to uk-UA.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("uk-UA");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());
				
				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
			else if (Thread.CurrentThread.CurrentUICulture.Name == "uk-UA")
			{
				// Changes the CurrentCulture of the current thread to en-US.
				Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
				ScreenManager.Screens.Clear();
				ScreenManager.AddScreen(new PhoneMainMenuScreen());
				ScreenManager.AddScreen(new OptionsMenuScreen());
			}
		}

		void scoreLimitButton_Tapped(object sender, EventArgs e)
		{
			scoreLimit += 5;
			if (scoreLimit > 25)
				scoreLimit = 5;
		}

        /// <summary>
        /// Updates the options screen, filling in the latest values for the menu text.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            scoreLimitButton.Text = StringData.score_limit_text + scoreLimit.ToString();
			localeButton.Text = StringData.locale_text;
			backButton.Text = StringData.back_text;

			#region old UPD
			//asteroidDensityMenuEntry.Text = "Asteroid Density : " + 
			//    asteroidDensity[currentAsteroidDensity];
			//wallStyleMenuEntry.Text = "Wall Style : " + wallStyle[currentWallStyle];
			//motionBlurMenuEntry.Text = "Motion Blur : " + motionBlur.ToString();
			//neonEffectMenuEntry.Text = "Neon Effect : " + neonEffect.ToString();

			//showMeshesMenuEntry.Text = "Show Meshes: " + showMeshes.ToString();
			#endregion
		}


        /// <summary>
        /// Event handler for when the Score Limit menu entry is selected.
        /// </summary>
        void ScoreLimitMenuEntrySelected(object sender, EventArgs e)
        {
            scoreLimit += 5;
            if (scoreLimit > 25)
                scoreLimit = 5;
        }


        /// <summary>
        /// Event handler for when the Asteroid Density menu entry is selected.
        /// </summary>
        void AsteroidDensityMenuEntrySelected(object sender, EventArgs e)
        {
            currentAsteroidDensity = (currentAsteroidDensity + 1) %
                asteroidDensity.Length;
        }


        /// <summary>
        /// Event handler for when the Wall Style menu entry is selected.
        /// </summary>
        void WallStyleMenuEntrySelected(object sender, EventArgs e)
        {
            currentWallStyle = (currentWallStyle + 1) % wallStyle.Length;
        }


        /// <summary>
        /// Event handler for when the Motion Blur menu entry is selected.
        /// </summary>
        void MotionBlurMenuEntrySelected(object sender, EventArgs e)
        {
            motionBlur = !motionBlur;
        }


        /// <summary>
        /// Event handler for when the NeonEffect menu entry is selected.
        /// </summary>
        void NeonEffectMenuEntrySelected(object sender, EventArgs e)
        {
            neonEffect = !neonEffect;
        }


        /// <summary>
        /// When the user cancels the options screen, go back to the main menu.
        /// </summary>
        protected override void OnCancel()
        {
            WorldRules.AsteroidDensity = 
                (AsteroidDensity)Enum.Parse(typeof(AsteroidDensity), 
                                         asteroidDensity[currentAsteroidDensity], true);
            WorldRules.WallStyle = (WallStyle)Enum.Parse(typeof(WallStyle), 
                wallStyle[currentWallStyle], true);
            WorldRules.ScoreLimit = scoreLimit;
            WorldRules.MotionBlur = motionBlur;
            WorldRules.NeonEffect = neonEffect;
            WorldRules.ShowMeshes = showMeshes;

            ExitScreen();
        }


        #endregion
    }
}
